package com.b2.controller.wx;

import com.b2.context.BaseContext;
import com.b2.pojo.entity.User;
import com.b2.properties.WeChatProperties;
import com.b2.service.UserService;
import com.b2.utils.AccessTokenUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
@Slf4j
@RequestMapping("/wx/push")
public class PushController {
    @Autowired
    WeChatProperties weChatProperties;
    @Autowired
    private UserService userService;



    /*
     * 微信测试账号推送
     * */
    @GetMapping("/pushUser")
    public void push() {
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        //获取appid
        String appid = weChatProperties.getAppid();
        //获取secret
        String secret = weChatProperties.getSecret();
        //获取accessToken
        String accessToken = AccessTokenUtils.getAccessTokenOne(appid, secret);
        wxStorage.setAppId(appid);
        wxStorage.setSecret(secret);
        wxStorage.setAccessToken(accessToken);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        Long userId = BaseContext.getCurrentId();
//        //获取推送给的用户
//        User user = userService.getOne(Wrappers.<User>lambdaQuery()
//                .eq(User::getId, userId));
//        //获取用户openid
//        String openid = user.getOpenid();
        String templateId = weChatProperties.getTemplateId();
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("owQTQ6S6evOi1VcqcLQAoiIgWIls")//要推送的用户openid
                .templateId(templateId)//模版id
                .build();

        //3.封装推送消息的数据
        templateMessage.addData(new WxMpTemplateData("course","数学","#173177"))
                .addData(new WxMpTemplateData("teacher","张三","#173177"))
                .addData(new WxMpTemplateData("text","数学课","#173177"))
                .addData(new WxMpTemplateData("time","2022-02-22 12:00","#173177"));
        //3,如果是正式版发送模版消息，这里需要配置你的信息
        //        templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF"));
        //                templateMessage.addData(new WxMpTemplateData(name2, value2, color2));
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }

    }




}
